package com.example.myblog1_0.service.Impl;

import com.example.myblog1_0.mapper.CategoriesMapper;
import com.example.myblog1_0.service.CategoryService;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: xc
 * @Date: 2018/12/16 21:07
 * @Description:
 **/
@Service
public class CategoryServiceImpl implements CategoryService{
    @Autowired
    CategoriesMapper categoriesMapper;

    @Override
    public JSONArray findCategoriesName() {
        List<String> categories = categoriesMapper.findCategoriesName();

        return JSONArray.fromObject(categories);
    }

    @Override
    public JSONObject getArticleNameAndNum() {
        List<String> categories = categoriesMapper.findCategoriesName();
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject;
        for (String category : categories){
            jsonObject = new JSONObject();
            jsonObject.put("categoryName",category);
            jsonObject.put("categoryArticleNum",categoriesMapper.getArticleNumByName(category));
            jsonArray.add(jsonObject);
        }
        JSONObject returnJson = new JSONObject();
        returnJson.put("status",200);
        returnJson.put("result",jsonArray);
        return returnJson;
    }
}
